<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Project;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Log;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;

class AdminUser extends Model
{
    const DEFAULT_ID = 1;

    protected $table = 'admin_users';

    public function projects():BelongsToMany
    {
        return $this->belongsToMany(Project::class,'xfy_project_users','user_id','projectId')->withTimestamps();
    }

    public function roles(): BelongsToMany
    {
        $pivotTable = config('admin.database.role_users_table');

        $relatedModel = config('admin.database.roles_model');

        return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'role_id')->withTimestamps();
    }

    public function getAvatar()
    {
        $avatar = $this->avatar;

        if ($avatar) {
            if (! URL::isValidUrl($avatar)) {
                $avatar = Storage::disk(config('admin.upload.disk'))->url($avatar);
            }

            return $avatar;
        }

        return admin_asset(config('admin.default_avatar') ?: '@admin/images/default-avatar.jpg');
    }

}